package com.quickdv.until;

import android.graphics.Bitmap;
import android.os.Environment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

/**
 *
 * Created by Administrator on 2015/7/24.
 */
public class FileUntil {

    public static int copy(String fromFile, String toFile)
    {
        File[] currentFiles;
        File root = new File(fromFile);
        if(!root.exists())
        {
            return -1;
        }
        currentFiles = root.listFiles();

        File targetDir = new File(toFile);
        if(!targetDir.exists())
        {
            targetDir.mkdirs();
        }
        for(int i= 0;i<currentFiles.length;i++)
        {
            if(currentFiles[i].isDirectory())
            {
                copy(currentFiles[i].getPath() + "/", toFile + currentFiles[i].getName() + "/");

            }else
            {
                CopySdcardFile(currentFiles[i].getPath(), toFile + currentFiles[i].getName());
            }
        }
        return 0;
    }


    private static int CopySdcardFile(String fromFile, String toFile)
    {

        try
        {
            InputStream fosfrom = new FileInputStream(fromFile);
            OutputStream fosto = new FileOutputStream(toFile);
            byte bt[] = new byte[1024];
            int c;
            while ((c = fosfrom.read(bt)) > 0)
            {
                fosto.write(bt, 0, c);
            }
            fosfrom.close();
            fosto.close();
            return 0;

        } catch (Exception ex)
        {
            return -1;
        }
    }

    public static void saveMyBitmap(String bitName,Bitmap mBitmap){
        File f = new File(bitName);
        File dir = new File(Environment.getExternalStorageDirectory() + "/cricle_pic");
        if(!dir.exists()){
            dir.mkdirs();
        }
        try {
            if(!f.exists()){
                f.createNewFile();
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
           e.printStackTrace();
        }
        FileOutputStream fOut = null;
        try {
            fOut = new FileOutputStream(f);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        mBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
        try {
            fOut.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            fOut.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
